<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :data="tableData"
      :loading="loading"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="name"
          title="分组名称"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.name" placeholder="分组名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="130"
          field="createdByName"
          title="创建人"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.createdByName" placeholder="创建人" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          show-header-overflow
          show-overflow
          min-width="420"
          field="gmtCreated"
          title="创建时间"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" min-width="100" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
    <vxe-modal v-model="modelAddFlag" title="新增" width="800" height="500" resize>
      <!-- modelAddFlag 解决宽度异常问题 -->
      <compAdd
        v-if="modelAddFlag"
        :restaurants="restaurants"
        @fatherAddMethod="fatherAdd"
        @fatherCancelMethod="fatherCancel"
      />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="800" height="500" resize>
      <compEdit
        ref="editRef"
        :restaurants="restaurants"
        @fatherEditMethod="fatherEdit"
        @fatherCancelMethod="fatherCancel"
      />
    </vxe-modal>
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, { mounts: ["cookie"] });
import compAdd from "./components/vehicleTeamAdd.vue";
import compEdit from "./components/vehicleTeamEdit.vue";
import {
  getInviteDriverList,
  getVehicleTeamList,
  delVehicleTeam
} from "@/api/shipment";
export default {
  name: "VehicleTeamList",
  components: {
    compAdd,
    compEdit
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      restaurants: [], // 司机列表
      loading: false,
      tableData: [],
      gmtCreatedRange: "",
      modelAddFlag: false,
      modelEditFlag: false,
      listQuery: {
        userCode: "",
        name: "",
        createdByName: "",
        gmtCreatedStart: "",
        gmtCreatedEnd: "",
        pageNum: 1,
        pageSize: 10,
        sortField: "",
        sortType: "",
        total: 0
      }
    };
  },
  created() {
    getInviteDriverList({ pageSize: 100000 }).then(response => {
      this.restaurants = response.data.records;
    });
    this.listQuery.userCode = this.$route.query.partyCode ? this.$route.query.partyCode : "";
    this.findList();
  },
  methods: {
    // 获取/查询 列表
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        getVehicleTeamList(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.loading = false;
          resolve()
        });
      })
    },
    changeDate(value) {
      if (value) {
        this.listQuery.gmtCreatedStart = value[0] + " 00:00:00";
        this.listQuery.gmtCreatedEnd = value[1] + " 23:59:59";
      } else {
        this.listQuery.gmtCreatedStart = "";
        this.listQuery.gmtCreatedEnd = "";
      }
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      var str = property === "createdByName" ? "createdBy" : property;
      if (order) {
        this.listQuery.sortField = str;
        this.listQuery.sortType = order;
      } else {
        this.listQuery.sortField = "";
      }
      this.findList();
    },
    // 新增
    funcAdd() {
      this.modelAddFlag = true;
    },
    // 编辑
    funcEdit(row) {
      this.modelEditFlag = true;
      this.$refs.editRef.getInfo(row.name, row.code);
    },
    // 删除方法
    funcDelete(codes) {
      this.$confirm("确认删除吗？")
        .then(_ => {
          delVehicleTeam({ teamCodes: codes }).then(response => {
            this.$message({ message: response.msg, type: "success" });
            this.findList();
          });
        })
        .catch(_ => {});
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false;
      this.findList();
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false;
      this.findList();
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case "add":
          this.modelAddFlag = false;
          break;
        case "edit":
          this.modelEditFlag = false;
          break;
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    }
  }
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
